package com.example.demo.controller;
import com.example.demo.api.CommonResult;
import com.example.demo.api.ResultCode;
import com.example.demo.service.TokenService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;


@RestController
@RequestMapping("/house/v1")
public class PortraitController {

    @Autowired
    private TokenService tokenservice;

    /**
     * 上传头像
     */
    @PostMapping("/pic/{username}/{token}")
    public CommonResult getPic(@RequestParam(required = true) MultipartFile[] file,
                               @PathVariable(name="username") String username,
                               @PathVariable(name="token") String token) {
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                File dir = new File("portrait/"+username);
                if(!dir.exists()) {
                    dir.mkdirs();
                }

                for (MultipartFile f:file) {
                    String imgName = f.getOriginalFilename();
                    String path = dir.getAbsolutePath() + "\\" + imgName;
                    System.out.println(imgName);
                    System.out.println(path);
                    //文件实现上传
                    f.transferTo(new File(path));
                }
                return CommonResult.result(ResultCode.PICTURE_SUCCESS.getCode(), ResultCode.PICTURE_SUCCESS.getMessage());
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            return CommonResult.result(ResultCode.PICTURE_FAILED.getCode(), ResultCode.PICTURE_FAILED.getMessage());
        }
    }

//    /**
//    *   下载头像
//     */
//    @GetMapping("/pic/{username}/{token}")
//    public CommonResult postPic(@PathVariable(name="username") String username,
//                          @PathVariable(name="token") String token) {
//        try{
//            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
//                File dir = new File("portrait/" + username);
//                File[] flist = null;
//                if (dir.exists() && dir.isDirectory()) {
//                    flist = dir.listFiles();
//                    for (File f : flist) {
//                        System.out.println(f.length());
//                    }
//                }
//                return CommonResult.result(ResultCode.PICTURE_SUCCESS.getCode(), ResultCode.PICTURE_SUCCESS.getMessage(), flist);
//            }else{
//                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
//            }
//        } catch(Exception e) {
//            e.printStackTrace();
//            return CommonResult.result(ResultCode.PICTURE_FAILED.getCode(), ResultCode.PICTURE_FAILED.getMessage());
//        }
//    }

    @GetMapping("/pic")
    public byte[] pic() {
        File file = new File("C:\\Users\\skywingsir\\Desktop\\头像1.jpeg");
        byte[] data = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            data = baos.toByteArray();

            fis.close();
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(Arrays.toString(data));
        return data;
    }

}
